package com.itheima.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.QuestionDto;
import com.itheima.admin.dto.QuestionItemDto;
import com.itheima.admin.pojo.Question;
import com.itheima.admin.pojo.QuestionItem;
import com.itheima.admin.vo.QuestionVo;
import com.itheima.common.contants.RedisPicConstants;
import com.itheima.question.mapper.QuestionItemMapper;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName QuestionServiceImpl
 * @Description TODO
 * @Author Mister-Lu
 * @Date 2021/12/21
 **/
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private QuestionItemMapper questionItemMapper;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public Result questionAdd(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto,question);
        int insert = questionMapper.insert(question);

        String id = question.getId();
        String pid = question.getPicture();
        //保存redis
        Long add = redisTemplate.opsForSet().add(RedisPicConstants.AUDIT_QUESTION, id);
        System.out.println("aaa"+add+"--"+id+"---");
        Long add1 = redisTemplate.opsForSet().add(RedisPicConstants.USE_PIC, pid.replace(fileServerUrl, ""));
        System.out.println("bbb"+add1);
        return new Result(insert>0,insert>0?"添加成功":"添加失败",null);
    }

    @Override
    public QuestionDto queryById(String id) {
        Question question = questionMapper.selectById(id);
        QuestionDto questionDto = new QuestionDto();
        BeanUtils.copyProperties(question, questionDto);
        //System.out.println(questionDto.getSubject());
        return questionDto;
    }


    @Override
    public PageVo<QuestionVo> queryByPage(PageDto pageDto) {
        IPage<Question> page = new Page(pageDto.getCurrentPage(),pageDto.getPageSize());
        QueryWrapper<Question> queryWrapper=null;
        if(pageDto.getQueryString()!=null){
            queryWrapper = new QueryWrapper<>();
            queryWrapper.like("subject",pageDto.getQueryString());
        }
        IPage<Question> questionIPage = questionMapper.selectPage(page, queryWrapper);
        List<QuestionVo> collect = getQuestionDtos(questionIPage.getRecords());
        return new PageVo<QuestionVo>(collect, (int) questionIPage.getTotal());
    }

    private List<QuestionVo> getQuestionDtos(List<Question> questionIPage) {
        List<QuestionVo> collect=null;
        if(questionIPage!=null){
            collect = questionIPage.stream().map(question -> {
                QuestionVo questionVo = new QuestionVo();
                BeanUtils.copyProperties(question,questionVo);
                return questionVo;
            }).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public Result editQuestionById(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto,question);
        int insert = questionMapper.updateById(question);
        return new Result(insert>0,insert>0?"编辑题目成功":"编辑题目失败",null);
    }

    @Override
    public Result delById(String id) {
        int insert = questionMapper.deleteById(id);
        return new Result(insert>0,insert>0?"删除题目成功":"删除题目失败",null);
    }

    @Override
    public QuestionItemDto queryItemById(String questionId) {
        QuestionItem questionItem = questionItemMapper.selectById(questionId);
        QuestionItemDto questionItemDto = new QuestionItemDto();
        BeanUtils.copyProperties(questionItem,questionItemDto);
        return questionItemDto;
    }

    @Override
    public Result questionAdd2(QuestionItemDto questionItemDto) {
        QuestionItem questionitem = new QuestionItem();
        BeanUtils.copyProperties(questionItemDto,questionitem);
        int insert = questionItemMapper.insert(questionitem);

        String questionId = questionitem.getQuestionId();
        //保存redis
        Long add = redisTemplate.opsForSet().add(RedisPicConstants.AUDIT_QUESTION, questionId);
        System.out.println("aaa"+add+"--"+questionId+"---");
        return new Result(insert>0,insert>0?"添加成功":"添加失败",null);
    }

}
